package domfarr.model;

import java.io.Serializable;

import javax.persistence.Embeddable;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * Entity for Name
 * 
 * @author Dominic Farr
 * 
 */
@Embeddable
public final class Name implements Serializable {

	private static final long serialVersionUID = 1L;

	private String title;

	private String firstName;

	private String initial;

	private String lastName;

	public Name(final String title, final String firstName,
			final String initial, final String lastName) {
		setTitle(title);
		setFirstName(firstName);
		setInitial(initial);
		setLastName(lastName);
	}

	public Name(final String firstName, final String lastName) {
		this("", firstName, "", lastName);
	}

	public Name() {
		this("", "", "", "");
	}

	public String getFirstName() {
		return firstName;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getInitial() {
		return initial;
	}

	public void setInitial(String initial) {
		this.initial = initial;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * Returns the full name by appending the first name, initial, and last name
	 * together.
	 * 
	 * @return the full name
	 */
	public String getFullName() {
		StringBuffer stringBuffer = new StringBuffer();

		if (!StringUtils.isBlank(title)) {
			stringBuffer.append(title).append(" ");
		}
		stringBuffer.append(firstName).append(" ");
		if (!StringUtils.isBlank(initial)) {
			stringBuffer.append(initial).append(" ");
		}
		stringBuffer.append(lastName);

		return stringBuffer.toString();
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}

	@Override
	public boolean equals(Object rhs) {
		return EqualsBuilder.reflectionEquals(this, rhs);
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}

}
